﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using EazyCMS.Common;

namespace EazyCMS.Web.Plugin.Naire.admin
{
    public partial class index : EazyCMS.Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string site = string.Empty;
        protected string id = string.Empty;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.site = EazyRequest.GetQueryString("site");
            this.keywords = EazyRequest.GetQueryString("keywords");
            this.id = EazyRequest.GetQueryString("id");
            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("plugin_vote", EazyEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind(); //绑定站点
                RptBind("id>0" + CombSqlTxt(this.site, this.keywords,this.id), "sort_id asc,add_time desc"); //绑定数据
            }
        }

        #region 绑定站点=================================
        private void TreeBind()
        {
            int parent_id = 0;
            EazyCMS.BLL.article_category bllzt = new EazyCMS.BLL.article_category();
            EazyCMS.BLL.channel_site bll = new EazyCMS.BLL.channel_site();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id desc").Tables[0];

            this.ddlSitePath.Items.Clear();
            this.ddlSitePath.Items.Add(new ListItem("所有站点", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlSitePath.Items.Add(new ListItem(dr["title"].ToString(), dr["build_path"].ToString()));
            }
         
        }
      
        #endregion
        //批量加入专题
        protected void btnSpecial_Click(object sender, EventArgs e)
        {
            //ChkAdminLevel("channel_" + this.channel_name + "_list", EazyEnums.ActionEnum.Audit.ToString()); //检查权限
            //BLL.article bll = new BLL.article();
            //Repeater rptList = new Repeater();
            //switch (this.prolistview)
            //{
            //    case "Txt":
            //        rptList = this.rptList1;
            //        break;
            //    default:
            //        rptList = this.rptList2;
            //        break;
            //}
            //if (ddspecial.SelectedValue == "0")
            //{
            //    JscriptMsg("请选择相应的专题", "");
            //    return;
            //}
            //for (int i = 0; i < rptList.Items.Count; i++)
            //{
            //    int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
            //    CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
            //    if (cb.Checked)
            //    {
            //        bll.UpdateField(id, "special_id=" + ddspecial.SelectedValue, "");
            //    }
            //}
            //AddAdminLog(EazyEnums.ActionEnum.Audit.ToString(), "审核" + this.channel_name + "频道内容信息"); //记录日志
            //JscriptMsg("批量加入专题成功！", Utils.CombUrlTxt("article_list.aspx", "channel_id={0}&category_id={1}&keywords={2}&property={3}",
            //    this.channel_id.ToString(), this.category_id.ToString(), this.keywords, this.property));
        }
        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            if (!int.TryParse(Request.QueryString["page"] as string, out this.page))
            {
                this.page = 1;
            }
            this.txtKeywords.Text = this.keywords;
            BLL.vote bll = new BLL.vote();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("index.aspx", "site={0}&keywords={1}&page={2}", this.site, this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _site, string _keywords,string _special)
        {
            StringBuilder strTemp = new StringBuilder();
            _site = _site.Replace("'", "");
            _keywords = _keywords.Replace("'", "");
          
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like '%" + _keywords + "%' or content like '%" + _keywords + "%')");
            }
            if (_special != "0" & !string.IsNullOrEmpty(_special))
            {
                strTemp.Append(" and naire_id=" + _special);
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量=============================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("vote_page_size", "EazyCMSPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //筛选站点
        protected void ddlSitePath_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("index.aspx", "site={0}&keywords={1}", ddlSitePath.SelectedValue, this.keywords));
        }

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("index.aspx", "site={0}&keywords={1}&id={2}", this.site, txtKeywords.Text,this.id));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("vote_page_size", "EazyCMSPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("index.aspx", "site={0}&keywords={1}", this.site, this.keywords));
        }

        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("plugin_vote", EazyEnums.ActionEnum.Edit.ToString()); //检查权限
            BLL.vote bll = new BLL.vote();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            AddAdminLog(EazyEnums.ActionEnum.Edit.ToString(), "保存在线调查排序"); //记录日志
            JscriptMsg("保存排序成功！", Utils.CombUrlTxt("index.aspx", "site={0}&keywords={1}", this.site, this.keywords));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("plugin_vote", EazyEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.vote bll = new BLL.vote();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(EazyEnums.ActionEnum.Delete.ToString(), "删除在线调查数据成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", 
                Utils.CombUrlTxt("index.aspx", "site={0}&keywords={1}", this.site, this.keywords));
        }

    }
}